// main for books version of contract program using a list
// file:   ch8tst5.cpp

#include <iostream.h>
#include <conio.h> // holds code for function clrscr which clears the screen
#include "contrct2.h"
#include "ch8list2.h"


char GetProperty();
char GetOperatorChoice();
int GetContractID();

void DisplayInfo(Contract contract);
void ChangeInfo(Contract& contract);

void DisplayClosingMessage();
void DisplayIntro();

int GetSquareFootage();
int GetNumDesks();
int GetNumDays();

void ProcessUserChoices();
void InitializeInstances(ListOfContracts& contracts);

void DisplayContract(ListOfContracts& contracts);
void ChangeContract(ListOfContracts& contracts);
void DisplayAllContracts(ListOfContracts& contracts);
void AddContract(ListOfContracts& contracts);
void DeleteContract(ListOfContracts& contracts);
void CreateContract(int contractID, Contract& contract);

void main()
{
	DisplayIntro();
	ProcessUserChoices();
	DisplayClosingMessage();
}




char GetOperatorChoice()
{// please enter choice
// make a while loop and talk about redundancy.
	char choice;

	cout << "Please choose one of the following operations\n";
	cout << "         'D' to display the information for a contract\n";
	cout << "         'L' to display the information on ALL contracts\n";
	cout << "         'C' to change the information for a contract\n";
	cout << "         'A' to add a contract to the list\n";
	cout << "         'E' to eliminate a contract from the list\n";
	cout << "         'Q' to quit the program\n";
	cin >> choice;
	return choice;
}

char GetProperty()
{  char property;
	cout << "Please enter the letter corresponding to the property you want to change\n";
	cout << "          'S':  square Footage\n";
	cout << "          'K':  number of desks\n";
	cout << "          'Y':  number of days per week\n";
	cout << "          'N':  none\n";
	cin >> property;
	return property;
}

int GetContractID()
{// loop choice 1-5

	int num;
	cout << "\nPlease choose which contract you wish to work with\n";
	cout << "Choose 0 (zero) when you are finished with this operation.\n";
	cin >> num;
	return num;

}


void 	ChangeInfo(Contract& contract)
{
	char property;

	property = GetProperty();
	while ((property != 'N') && (property != 'n'))
	{	switch (property)
		{	case 'S':
			case 's':
				int sqFootage;
				sqFootage = GetSquareFootage();
				contract.ChangeSquareFootage(sqFootage);
				break;
			case 'K':
			case 'k':
				int numDesks;
				numDesks = GetNumDesks();
				contract.ChangeNumDesks(numDesks);
				break;
			case 'Y':
			case 'y':
				int numDays;
				numDays = GetNumDays();
				contract.ChangeNumDays(numDays);
				break;
			default:
				cout << "\nInvalid choice of properties. Please try again\n";

		}  // end switch
		property = GetProperty();
	}

	cout << "\nThe new Contract looks as follows:";
	DisplayInfo(contract);
}

void DisplayInfo(Contract contract)
{
	int contractNum = contract.ProvideID();
	int sqFootage;
	sqFootage = contract.ProvideSquareFootage();
	cout << "\n\nThe square footage for contract " << contractNum << " is: "
		  << sqFootage << endl;

	int numDesks;
	numDesks = contract.ProvideNumberOfDesks();
	cout << "The number of desks for the contract is: " << numDesks << endl;

	int numDays;
	numDays = contract.ProvideNumberOfDays();
	cout << "The number of days for the contract is: " << numDays << endl;

	double charge;
	charge = contract.ProvidePerWeekCharge();
	cout << "The per week charge for the contract is: " << charge << endl;
	cout << endl << endl;

}


void 	DisplayClosingMessage()
{	cout << "\n\n\n     H A V E   F U N";
}

void DisplayIntro()
{	cout << "               W E L CO M E   TO   O L Y M P I A ' S\n";
	cout << "         O F F I C E   C O N T R A C T   P R O G R A M\n\n\n";
}

int GetSquareFootage()
{	int sqFootage;
	cout << "\nWhat is the new square footage for the contract?";
	cin >> sqFootage;
	return sqFootage;
}


int GetNumDesks()
{	int numDesks;
	cout << "\nWhat is the new number of desks for the contract?";
	cin >> numDesks;
	return numDesks;
}


int GetNumDays()
{	int numDays;
	cout << "\nWhat is the new number of days for the contract?";
	cin >> numDays;
	return numDays;
}

void ProcessUserChoices()
{

// Code to create the list of contracts
ListOfContracts contracts;
InitializeInstances(contracts);

bool done = false;   // Is the user finished processing contracts?
char choice;

while (!done)
{
	choice = GetOperatorChoice();
	switch (choice)
		{	case 'D':
			case 'd':
				DisplayContract(contracts);
				break; // case 'd'
			case 'C':
			case 'c':
				ChangeContract(contracts);
				break; // case 'c'

			case 'L':
			case 'l':
				DisplayAllContracts(contracts);
				break;

			case 'A':
			case 'a':
				AddContract(contracts);
				break;

			case 'E':
			case 'e':
				DeleteContract(contracts);
				break;

			case 'Q':
			case 'q':
				cout << "Do you want to really want top quit (Y) or (N)\n";
				char answ;
				cin >> answ;
				if ((answ =='Y') || (answ == 'y'))
				{
					done = true;
				}

				break;
			default:
				cout << "Invalid choice. Please try again";

		}  // end switch
	}
}

void CreateContract(int contractID, Contract& contract)
{
	contract.ChangeID(contractID);

	int sqFootage;
	sqFootage = GetSquareFootage();
	contract.ChangeSquareFootage(sqFootage);

	int numDesks;
	numDesks = GetNumDesks();
	contract.ChangeNumDesks(numDesks);

	int numDays;
	numDays = GetNumDays();
	contract.ChangeNumDays(numDays);

	clrscr();
	DisplayInfo(contract);
}

void DisplayContract(ListOfContracts& contracts)
{ int contractID;
  bool found;
  Contract contract;
  contractID = GetContractID();
  while (contractID != 0)
  {  found = contracts.FindContract(contractID, contract);
	  if (found)
	  {  clrscr();
		  DisplayInfo(contract);
	  }
	  else
	  {	cout << "\nContract " << contractID << " is not in list.\n";
	  }
	  contractID = GetContractID();
	}
}

void ChangeContract(ListOfContracts& contracts)
{  int contractID;
	bool found;
	Contract contract;
	contractID =GetContractID();
	while (contractID != 0)
	{  found = contracts.FindContract(contractID, contract);
		if (found)
		{
			ChangeInfo(contract);
			contracts.ReplaceContract(contract);
		}
		else
		{	cout << "\nContract " << contractID << " is not in list.\n";
		}
		contractID = GetContractID();
	}
}

void DisplayAllContracts(ListOfContracts& contracts)
{ contracts.DisplayAll();
}

void AddContract(ListOfContracts& contracts)
{  int contractID;
	contractID = GetContractID();
	Contract contract;
	while (contractID != 0)   // The user wants to add another contract
	{	CreateContract(contractID, contract);
		contracts.Add(contract);
		contractID = GetContractID();
	}
}

void DeleteContract(ListOfContracts& contracts)
{  int contractID;
	contractID = GetContractID();
	while (contractID != 0) // The user wants to delete a contract
	{	contracts.Delete(contractID);
		contractID = GetContractID();
	}
}


void InitializeInstances(ListOfContracts& contracts)
{	// In a real program this info would be read in from a file.
	// In this test code we will hard code in the initial values.

	Contract contract;

	contract.ChangeID(1111);
	contract.ChangeSquareFootage(1000);
	contract.ChangeNumDesks(3);
	contract.ChangeNumDays(5);

	contracts.Add(contract);

	contract.ChangeID(2222);
	contract.ChangeSquareFootage(500);
	contract.ChangeNumDesks(10);
	contract.ChangeNumDays(3);

	contracts.Add(contract);

	contract.ChangeID(3333);
	contract.ChangeSquareFootage(200);
	contract.ChangeNumDesks(1);
	contract.ChangeNumDays(4);

	contracts.Add(contract);

	contract.ChangeID(4444);
	contract.ChangeSquareFootage(2000);
	contract.ChangeNumDesks(20);
	contract.ChangeNumDays(5);

	contracts.Add(contract);

	contract.ChangeID(5555);
	contract.ChangeSquareFootage(300);
	contract.ChangeNumDesks(2);
	contract.ChangeNumDays(2);

	contracts.Add(contract);
}